package com.dlc.shop.multishop.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dlc.shop.bean.dto.AllotOrderDTO;
import com.dlc.shop.bean.vo.AllotOrderVO;
import com.dlc.shop.bean.vo.AllotSpuImportVO;
import com.dlc.shop.bean.vo.AllotSpuInboundImportVO;
import com.dlc.shop.common.enums.SysTypeEnum;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.security.common.util.AuthUserContext;
import com.dlc.shop.service.AllotOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author gaozijie
 * @since 2023-11-20
 */
@Tag(name = "调拨订单接口")
@RestController
@AllArgsConstructor
@RequestMapping("/allotOrder")
public class AllotOrderController {

    private final AllotOrderService allotOrderService;

    @Operation(summary = "分页查询调拨订单")
    @GetMapping("/page")
    public ServerResponseEntity<Page<AllotOrderVO>> pageAllotOrder(PageParam<AllotOrderVO> pageParam,
                                                                   AllotOrderDTO allotOrderDTO) {
        allotOrderDTO.setShopId(AuthUserContext.get().getShopId());
        Page<AllotOrderVO> page = allotOrderService.pageAllotOrder(pageParam, allotOrderDTO);
        return ServerResponseEntity.success(page);
    }

    @Operation(summary = "获取调拨订单详情")
    @Parameter(name = "allotOrderId", description = "调拨订单id")
    @GetMapping("/info/{allotOrderId}")
    public ServerResponseEntity<AllotOrderVO> getAllotOrder(@PathVariable("allotOrderId") Long allotOrderId) {
        AllotOrderDTO allotOrderDTO = this.getAllotOrderDTO();
        allotOrderDTO.setAllotOrderId(allotOrderId);
        AllotOrderVO allotOrderVO = allotOrderService.getAllotOrder(allotOrderDTO);
        return ServerResponseEntity.success(allotOrderVO);
    }

    @Operation(summary = "新增调拨订单")
    @PostMapping
    public ServerResponseEntity<Void> addAllotOrder(@RequestBody AllotOrderDTO allotOrderDTO) {
        allotOrderDTO.setShopId(AuthUserContext.get().getShopId());
        allotOrderDTO.setUserId(AuthUserContext.get().getUserId());
        allotOrderDTO.setSysType(SysTypeEnum.MULTISHOP.value());
        allotOrderService.addAllotOrder(allotOrderDTO);
        return ServerResponseEntity.success();
    }

    @Operation(summary = "入库调拨订单")
    @PutMapping("/inbound")
    public ServerResponseEntity<Void> inbound(@RequestBody AllotOrderDTO allotOrderDTO) {
        allotOrderDTO.setShopId(AuthUserContext.get().getShopId());
        allotOrderDTO.setUserId(AuthUserContext.get().getUserId());
        allotOrderDTO.setSysType(SysTypeEnum.MULTISHOP.value());
        allotOrderService.inbound(allotOrderDTO);
        return ServerResponseEntity.success();
    }

    @Operation(summary = "完成调拨订单")
    @Parameter(name = "allotOrderId", description = "调拨订单id")
    @PutMapping("/complete/{allotOrderId}")
    public ServerResponseEntity<Void> finish(@PathVariable("allotOrderId") Long allotOrderId) {
        AllotOrderDTO allotOrderDTO = this.getAllotOrderDTO();
        allotOrderDTO.setAllotOrderId(allotOrderId);
        allotOrderService.completeOrder(allotOrderDTO);
        return ServerResponseEntity.success();
    }

    @Operation(summary = "作废调拨订单")
    @Parameter(name = "allotOrderId", description = "调拨订单id")
    @DeleteMapping("/nullify/{allotOrderId}")
    public ServerResponseEntity<Void> nullify(@PathVariable("allotOrderId") Long allotOrderId) {
        AllotOrderDTO allotOrderDTO = this.getAllotOrderDTO();
        allotOrderDTO.setAllotOrderId(allotOrderId);
        allotOrderService.nullifyOrder(allotOrderDTO);
        return ServerResponseEntity.success();
    }

    @Operation(summary = "下载商品excel模板")
    @GetMapping("/downloadModel")
    public void downloadModel(HttpServletResponse response) {
        allotOrderService.downloadModel(response);
    }

    @Operation(summary = "导入商品excel")
    @Parameters({
            @Parameter(name = "outWarehouseId", description = "调出仓库id"),
            @Parameter(name = "inWarehouseId", description = "调入仓库id")
    })
    @PostMapping("/importExcel")
    public ServerResponseEntity<AllotSpuImportVO> importExcel(Long outWarehouseId,
                                                              Long inWarehouseId,
                                                              @RequestPart("excelFile") MultipartFile excelFile) {
        AllotOrderDTO allotOrderDTO = this.getAllotOrderDTO();
        allotOrderDTO.setOutWarehouseId(outWarehouseId);
        allotOrderDTO.setInWarehouseId(inWarehouseId);
        return ServerResponseEntity.success(allotOrderService.parseFile(excelFile, allotOrderDTO));
    }

    @Operation(summary = "下载商品入库excel模板")
    @Parameter(name = "allotOrderId", description = "调拨订单id")
    @GetMapping("/downloadInboundModel")
    public void downloadInboundModel(HttpServletResponse response, Long allotOrderId) {
        AllotOrderDTO allotOrderDTO = this.getAllotOrderDTO();
        allotOrderDTO.setAllotOrderId(allotOrderId);
        allotOrderService.downloadInboundModel(response, allotOrderDTO);
    }

    @Operation(summary = "导入商品入库excel")
    @Parameter(name = "allotOrderId", description = "调拨订单id")
    @PostMapping("/importInboundExcel")
    public ServerResponseEntity<AllotSpuInboundImportVO> importInboundExcel(Long allotOrderId,
                                                                            @RequestPart("excelFile") MultipartFile excelFile) {
        AllotOrderDTO allotOrderDTO = this.getAllotOrderDTO();
        allotOrderDTO.setAllotOrderId(allotOrderId);
        return ServerResponseEntity.success(allotOrderService.parseInboundFile(excelFile, allotOrderDTO));
    }

    private AllotOrderDTO getAllotOrderDTO() {
        AllotOrderDTO allotOrderDTO = new AllotOrderDTO();
        allotOrderDTO.setShopId(AuthUserContext.get().getShopId());
        allotOrderDTO.setUserId(AuthUserContext.get().getUserId());
        allotOrderDTO.setSysType(SysTypeEnum.MULTISHOP.value());
        return allotOrderDTO;
    }
}
